package com.clinic.accessor.db;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.clinic.entity.db.Appointment;
import com.clinic.entity.db.User;
import com.clinic.entity.param.Index;

@Component
public class AppointmentAccessor extends AbstractDbAccessor<Appointment> {

	public Appointment getAppointmentById(long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Appointment.class);
		criteria.add(Restrictions.eq(Appointment.Columns.APPOINTMENT_ID, id));
		return uniqueGeneric(criteria);
	}
	
	public List<Appointment> getAppointmentsByPatientId(long userId, Index index) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Appointment.class);
		criteria.add(Restrictions.eq("patient." + User.Columns.USER_ID, userId));
		return listGeneric(criteria, index);
	}
	
	public List<Appointment> getFutureOrOnDateAppointments(Date sdate, Index index) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Appointment.class);
		criteria.add(Restrictions.gt(Appointment.Columns.DATE_AND_TIME, sdate));
		return listGeneric(criteria, index);
	}

	public Appointment getUpcomingPatientAppointment(long userId, Date sdate) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Appointment.class);
		criteria.add(Restrictions.eq("patient." + User.Columns.USER_ID, userId));
		criteria.add(Restrictions.gt(Appointment.Columns.DATE_AND_TIME, sdate));
		return uniqueGeneric(criteria);
	}
	
	public Appointment getPatientAppointmentByDate(long userId, Date sdate, Date edate) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Appointment.class);
		criteria.add(Restrictions.eq("patient." + User.Columns.USER_ID, userId));
		criteria.add(Restrictions.gt(Appointment.Columns.DATE_AND_TIME, sdate));
		criteria.add(Restrictions.lt(Appointment.Columns.DATE_AND_TIME, edate));
		return uniqueGeneric(criteria);
	}
	
	public List<Appointment> getAllAppointments() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Appointment.class);
		criteria.add(Restrictions.gt(Appointment.Columns.APPOINTMENT_ID, 0L));
		return listGeneric(criteria, new Index());
	}
	
	public List<Appointment> getAppointmentsByDate(Date sdate, Date edate) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Appointment.class);
		criteria.add(Restrictions.gt(Appointment.Columns.DATE_AND_TIME, sdate));
		criteria.add(Restrictions.lt(Appointment.Columns.DATE_AND_TIME, edate));
		return listGeneric(criteria, new Index(20));
	}
	
}
